const toString = Object.prototype.toString
const checkTypeFn = (type) => {
  return function isType(value) {
    return toString.call(value) === `[object ${type}]`
  }
}

export const isFunc = checkTypeFn('Function')
export const isAsyncFunc = checkTypeFn('AsyncFunction')
export const isUndefined = checkTypeFn('Undefined')
export const isArray = checkTypeFn('Array')
export const isString = checkTypeFn('String')
export const isObject = checkTypeFn('Object')
export const isNumber = checkTypeFn('Number')
export const isDate = checkTypeFn('Date')
export const isBoolean = checkTypeFn('Boolean')
export const isHtmlElement = (node) => node && node.nodeType === Node.ELEMENT_NODE
export const isDefined = (val) => val !== undefined && val !== null
export const isNull = (data) => {
  if (!data) return true
  if (!Reflect.ownKeys(data)?.length) return true
  if (!data.length || JSON.stringify(data) === '[]') return true
}

// 布尔类型转换
export const toBoolean = (value, truthyValues = ['true']) => {
  const normalizedValue = String(value).toLowerCase().trim()
  return truthyValues.includes(normalizedValue)
}

export const isStringNumber = (value) => {
  if (!isString(value)) {
    return false
  }
  return !Number.isNaN(Number(value))
}
